use "uncippo_final.dta", clear

keep if approved==1 | first_report_proposed_only==1

*drop UNTAC's unusually large deployment
drop if unit=="Additional Electoral support" // UNTAC unit w > 60,000

*FIGURE 1 - Overall Yearly Trends
preserve
collapse (max) usgasg unitednationsvolunteers d2d1 fieldservice generalservice nationalstaff p1 p3p2 p5p4 securityservice international total substantive supportst (first) umbrella_code1 umbrella1 country (first) iso, by(mission year unit)
collapse (sum) usgasg unitednationsvolunteers d2d1 fieldservice generalservice nationalstaff p1 p3p2 p5p4 securityservice international total substantive supportst, by(mission year)
collapse (sum) usgasg unitednationsvolunteers d2d1 fieldservice generalservice nationalstaff p1 p3p2 p5p4 securityservice international total substantive supportst, by(year)
label var international "International Posts"
label var total "Total Posts"
label var national "National Posts"
label var substantive "Substantive Posts"
label var support "Support Posts"
tsset year
twoway (tsline international, lcolor(gs12)) (tsline national, lpattern(-) lcolor(gs12)) (tsline total, lcolor(black)) , ytitle(Civilian Posts) legend(size(*1.5))
twoway (tsline substantive, lcolor(gs12)) (tsline supportst, lpattern(-) lcolor(gs12)) (tsline total, lcolor(black)) , ytitle(Civilian Posts) legend(size(*1.5))
restore



*FIGURE 2
collapse (max) usgasg unitednationsvolunteers d2d1 fieldservice generalservice nationalstaff p1 p3p2 p5p4 securityservice international total substantive supportst (first) umbrella_code1 umbrella1 country iso, by(mission year unit)  
collapse (sum) usgasg unitednationsvolunteers d2d1 fieldservice generalservice nationalstaff p1 p3p2 p5p4 securityservice international total substantive supportst (first) umbrella1 country iso, by(mission year umbrella_code1)

rename total totalstaff
drop umbrella1
drop if umbrella_code1==.
reshape wide usg-supportstaff, i(mission country year) j(umbrella_code1)
gen childprotection_staff=totalstaff1
gen civilaffairs_staff=totalstaff2
gen ddr_staff=totalstaff4
gen electoralaffairs_staff=totalstaff5
gen gender_staff=totalstaff6
gen hiv_staff=totalstaff7
gen humanrights_staff=totalstaff8
gen humanitarian_staff=totalstaff9
gen rol_staff=totalstaff11
gen poc_staff=totalstaff12
gen politicalaffairs_staff=totalstaff13
gen publicinfo_staff=totalstaff14
gen ssr_staff=totalstaff17
gen stabilization_staff=totalstaff18
gen ceasefire_staff=totalstaff19
gen support_staff=totalstaff21
gen demining_staff=totalstaff22
gen childprotection_intstaff=internationalstaff1
gen civilaffairs_intstaff=internationalstaff2
gen ddr_intstaff=internationalstaff4
gen electoralaffairs_intstaff=internationalstaff5
gen gender_intstaff=internationalstaff6
gen hiv_intstaff=internationalstaff7
gen humanrights_intstaff=internationalstaff8
gen humanitarian_intstaff=internationalstaff9
gen rol_intstaff=internationalstaff11
gen poc_intstaff=internationalstaff12
gen politicalaffairs_intstaff=internationalstaff13
gen publicinfo_intstaff=internationalstaff14
gen ssr_intstaff=internationalstaff17
gen stabilization_intstaff=internationalstaff18
gen ceasefire_intstaff=internationalstaff19
gen sanctions_intstaff=internationalstaff20
gen support_intstaff=internationalstaff21
gen demining_intstaff=internationalstaff22
gen childprotection_natstaff=nationalstaff1
gen civilaffairs_natstaff=nationalstaff2
gen ddr_natstaff=nationalstaff4
gen electoralaffairs_natstaff=nationalstaff5
gen gender_natstaff=nationalstaff6
gen hiv_natstaff=nationalstaff7
gen humanrights_natstaff=nationalstaff8
gen humanitarian_natstaff=nationalstaff9
gen rol_natstaff=nationalstaff11
gen poc_natstaff=nationalstaff12
gen politicalaffairs_natstaff=nationalstaff13
gen publicinfo_natstaff=nationalstaff14
gen seniorcivilian_natstaff=nationalstaff15
gen ssr_natstaff=nationalstaff17
gen stabilization_natstaff=nationalstaff18
gen ceasefire_natstaff=nationalstaff19
gen sanctions_natstaff=nationalstaff20
gen support_natstaff=nationalstaff21
gen demining_natstaff=nationalstaff22

order support_*, last
foreach var of varlist childprotection_staff-support_natstaff{
	replace `var'=0 if `var'==.
}
keep country mission year childprotection_staff-support_natstaff
rename year year_budget
gen year = year_budget + 1
replace mission="UNCSPG" if mission=="UNPSG"
replace mission="UNOSOM I" if mission=="UNOSOM" & year==1992
replace mission="UNOSOM II" if mission=="UNOSOM" & year>1992
replace mission="UNOMOZ" if mission=="ONUMOZ"

*Filling missing budgets with previous year approved
egen missionid=group(mission)
tsset missionid year
tsfill
foreach var of varlist mission country childprotection_staff-support_natstaff missionid {
	cap replace `var'=`var'[_n-1] if `var'==.
	cap replace `var'=`var'[_n-1] if `var'==""

}

replace year_b=year-1 if year_b==.
merge m:1 mission year using "descriptives/pema.dta"
drop if _m!=3 // some cases in UNCIPPO are not in PEMA and viceversa
cap drop missionid 
egen missionid=group(mission)
egen total_staff= rowtotal(childprotection_staff-demining_staff) // excludes support
egen total_mand=rowtotal(childprotection_sum-demining_sum) // excludes support
bysort year: egen y_total_staff= sum(total_staff) 
bysort year: egen y_total_mand= sum(total_mand) 
bysort year: egen y_mean_staff= mean(total_staff) 
bysort year: egen y_mean_mand= mean(total_mand) 
egen total_intstaff= rowtotal(childprotection_intstaff-demining_intstaff) 
egen total_natstaff= rowtotal(childprotection_natstaff-demining_natstaff) 
bysort year: egen y_total_intstaff= sum(total_intstaff) 
bysort year: egen y_total_natstaff= sum(total_natstaff) 
bysort year: egen y_mean_intstaff= mean(total_intstaff) 
bysort year: egen y_mean_natstaff= mean(total_natstaff) 
gen ry_total_intstaff=y_total_intstaff/y_total_staff 
gen ry_total_natstaff=y_total_natstaff/y_total_staff 
gen r_total_intstaff=total_intstaff/total_staff 
gen r_total_natstaff=total_natstaff/total_staff 

drop if year>2017
foreach var in "childprotection" "civilaffairs" "ddr" "electoralaffairs" "gender" "hiv" "humanrights" "humanitarian"  "rol" "poc" "politicalaffairs" "publicinfo" "ssr" "stabilization" "ceasefire" "demining" {
	bysort year: egen y_`var'_staff=sum(`var'_staff) 
	bysort year: egen y_`var'_mand=sum(`var'_sum) 
	bysort year: egen y_`var'_staffmean=mean(`var'_staff) 
	bysort year: egen y_`var'_mandmean=mean(`var'_sum) 
	
	bysort year: egen y_`var'_staff2=sum(`var'_staff) if mission!="UNTAC"
	bysort year: egen y_`var'_mand2=sum(`var'_sum) if mission!="UNTAC"
	bysort year: egen y_`var'_staffmean2=mean(`var'_staff) if mission!="UNTAC"
	bysort year: egen y_`var'_mandmean2=mean(`var'_sum) if mission!="UNTAC"
}
 
bysort year: egen y_support_staff=sum(support_staff)
bysort year: egen y_support_staffmean=mean(support_staff)
foreach var in "childprotection" "civilaffairs"  "ddr" "electoralaffairs" "gender" "hiv" "humanrights" "humanitarian"  "rol" "poc" "politicalaffairs" "publicinfo"  "ssr" "stabilization" "ceasefire"  "demining" {
	 label var y_`var'_staff "Total Staff in `var'"
	 label var y_`var'_mand "Total Mandated Tasks in `var'"
}
	 label var y_support_staff "Total Staff in Support"	 
foreach var in "childprotection" "civilaffairs"  "ddr" "electoralaffairs" "gender" "hiv" "humanrights" "humanitarian"  "rol" "poc" "politicalaffairs" "publicinfo"  "ssr" "stabilization" "ceasefire"  "demining" "support" {
	 bysort year: egen y_`var'_natstaff=sum(`var'_natstaff) 
	 bysort year: egen y_`var'_intstaff=sum(`var'_intstaff) 
}
foreach var in "childprotection" "civilaffairs"  "ddr" "electoralaffairs" "gender" "hiv" "humanrights" "humanitarian"  "rol" "poc" "politicalaffairs" "publicinfo"  "ssr" "stabilization" "ceasefire"  "demining" "support" {
	bysort year: gen ry_`var'_natstaff=y_`var'_natstaff / (y_`var'_staff) if y_`var'_natstaff!=0
	bysort year: gen ry_`var'_intstaff=y_`var'_intstaff / (y_`var'_staff) if y_`var'_intstaff!=0
	replace ry_`var'_intstaff=0 if ry_`var'_intstaff==.
	replace  ry_`var'_natstaff=0 if  ry_`var'_natstaff==.
	}
cap egen missionid=group(mission)
twoway (tsline y_mean_staff, lcolor(gs12)) (tsline y_mean_mand, yaxis(2) lcolor(black)), legend(pos(6) col(2) order(1 "Budgeted Posts" 2 "Mandated Tasks") size(*1.5)) ytitle("Avg. Civilian Posts") ytitle("Avg. Tasks per Mission", axis(2)) xtitle("Year")

*FIGURE 3
twoway (tsline y_gender_mandmean, lcolor(black) lwidth(medium)) (tsline y_gender_staffmean, yaxis(2) lcolor(gs12) lwidth(medium)), legend(pos(6) col(2) order(1   "Mandated Tasks" 2 "Budgeted Posts") size(*1.5)) ytitle("Avg. Civilian Posts", axis(2)) ytitle("Avg. Tasks per Mission ") xtitle("Year") title("Gender")
twoway (tsline y_humanitarian_mandmean, lcolor(black) lwidth(medium)) (tsline y_humanitarian_staffmean, yaxis(2) lcolor(gs12) lwidth(medium)), legend(pos(6) col(2) order(1   "Mandated Tasks" 2 "Budgeted Posts") size(*1.5)) ytitle("Avg. Civilian Posts", axis(2)) ytitle("Avg. Tasks per Mission ") xtitle("Year")  title("Humanitarian Coordination")
twoway (tsline y_electoralaffairs_mandmean2, lcolor(black) lwidth(medium)) (tsline y_electoralaffairs_staffmean2, yaxis(2) lcolor(gs12) lwidth(medium)), legend(pos(6) col(2) order(1   "Mandated Tasks" 2 "Budgeted Posts") size(*1.5)) ytitle("Avg. Civilian Posts", axis(2)) ytitle("Avg. Tasks per Mission ") xtitle("Year")  title("Elections")
twoway (tsline y_humanrights_mandmean, lcolor(black) lwidth(medium)) (tsline y_humanrights_staffmean, yaxis(2) lcolor(gs12) lwidth(medium)), legend(pos(6) col(2) order(1   "Mandated Tasks" 2 "Budgeted Posts") size(*1.5)) ytitle("Avg. Civilian Posts", axis(2)) ytitle("Avg. Tasks per Mission ") xtitle("Year")  title("Human Rights")


*FIGURE 6
use "uncippo_final.dta", clear

sort country mission *year component section unit
keep if approved==1 | first_report_proposed_only==1

drop if unit=="Additional Electoral support" // UNTAC unit w > 60,000

preserve
replace umbrella_code1=11 if umbrella_code1==3
collapse (mean) usg-total international-professional, by(year) 
gen total_professional=usg+ d2+ p5 +p3 +p1 
gen total_international=total-nat
gen sh_usg=usg/total
gen sh_d=d2/total
gen sh_p=(p1+p3+p5)/total
gen sh_usg_p=usg/total_p
gen sh_d_p=d2/total_p
gen sh_p_p=(p1+p3+p5)/total_p
gen sh_prof=total_prof/total
gen sh_international=total_international/total
gen sh_fs=field/total
gen sh_gs=generalservice/total
gen sh_npo=national/total
gen sh_unv=unitednationsvolunteers/total

graph bar (asis) sh_fs sh_gs sh_npo sh_unv sh_prof, over(year, sort(year) lab(angle(45))) ytitle("% of Total Posts") legend(position(bottom) label(1 "Field Service") label(2 "General Service") label(3 "National") label(4 "UNV") label(5 "Professional"))  bar(1, color(black)) bar(2, color(gs5)) bar(3, color(gs8)) bar(4, color(gs12)) bar(5, color(gs14))  percentages stack 
graph bar (asis) sh_usg_p sh_d_p sh_p_p, over(year, sort(year) lab(angle(45))) ytitle("% of Professional Posts") legend(position(bottom) label(1 "ASG/USG") label(2 "D") label(3 "P"))  bar(1, color(gs4)) bar(2, color(gs12)) bar(3, color(gs14)) percentages stack
restore

*FIGURE 7
preserve
*keep if year>2004 
drop if support==1  | transit==1
collapse (mean) usg-total international-supportst (first) umbrella1 country, by(umbrella_code1) 
gen umbrella1_short=umbrella1
drop if umbrella1=="NA"
replace umbrella1_short="PoC" if umbrella_code1==12
replace umbrella1_short="Gender" if umbrella_code1==6
replace umbrella1_short="Humanitarian" if umbrella_code1==9
replace umbrella1_short="Justice" if umbrella_code1==11
replace umbrella1_short="Public Info" if umbrella_code1==14
replace umbrella1_short="Stablization" if umbrella_code1==18
replace umbrella1_short="Info Collect & Analysis" if umbrella_code1==10
replace umbrella1_short="Int. Management" if umbrella_code1==21
replace umbrella1_short="Sen. Civ. Leader" if umbrella_code1==15
replace umbrella1_short="Sen. Unif. Leader" if umbrella_code1==16
egen umbrescale=group(umbrella1_sh)
egen umbid=group(umbrescale)
labmask umbid, values(umbrella1_sh)
tab umbid
gen total_international=total-national 
gen total_professional=usg + d2 + p1 + p3 + p5 
gen sh_usg=usg/total
gen sh_d=d2/total
gen sh_p=(p1+p3+p5)/total
gen sh_usg_p=usg/total_p
gen sh_d_p=d2/total_p
gen sh_p_p=(p1+p3+p5)/total_p
gen sh_international=total_international/total
gen sh_fs=field/total
gen sh_gs=generalservice/total
gen sh_npo=national/total
gen sh_unv=unitednationsvolunteers/total
graph hbar (asis) sh_fs sh_gs sh_npo sh_unv sh_p, over(umbid, sort(sh_p)) ///
legen(position(bottom) label(1 "Field Service") label(2 "General Service") label(3 "National") label(4 "UNV") label(5 "Professional"))  bar(1, color(black)) bar(2, color(gs5)) bar(3, color(gs8)) bar(4, color(gs12)) bar(5, color(gs14))  /// 
 ytitle ("% of Total Posts")percentages stack
restore


*FIGURE 8
use "uncippo_final.dta", clear

sort country mission *year component section unit
keep if approved==1 | first_report_proposed_only==1

replace internationalst=total-nationalst-unitednationsvolunteers
gen national_temp=nationalst if temp==1
gen international_temp=international if temp==1
gen unv=unitednationsvolunteers if temp==0
gen unv_temp=unitednationsvolunteers if temp==1
replace nationalstaff=. if national_temp!=.
replace internationalst=. if international_temp!=.
keep country iso3n cown mission budget_year nationalstaff unv* internationalst total year approved temporary national_temp international_temp vr_*
foreach var of varlist nationalstaff unv unv_temp internationalst total national_temp international_temp{
	replace `var'=0 if `var'==.
}
collapse (sum) nationalstaff unv* internationalst total national_temp international_temp (mean) vr_* (first) iso3n, by(mission year)
rename nationalst national
replace mission = subinstr(mission, " ", "", .)

keep if mission=="MONUC" | mission=="MONUSCO"
keep if year>2004
gen est_int=internationalst - ((internationalst/100)*(vr_international))
gen est_nat=national - ((national/100)*(vr_national))
gen est_unv=unv - ((unv/100)*(vr_unv))

twoway bar internationalst year, barw(0.4) bc(gray) ///
|| bar est_int year, barw(0.4) bc(black) ytitle(International Civilian Posts) xtitle(Year) xla(, tlc(none)) yscale(range(0 1200)) legend(label(1 "Budgeted Posts") label(2 "Filled Posts (estimated)")) title("MONUC/MONUSCO")

twoway bar national year, barw(0.4) bc(gray) ///
|| bar est_nat year, barw(0.4) bc(black) ytitle(National Civilian Posts) xtitle(Year) xla(, tlc(none)) yscale(range(0 1200)) legend(label(1 "Budgeted Posts") label(2 "Filled Posts (estimated)")) title("MONUC/MONUSCO")

twoway bar unv year, barw(0.4) bc(gray) ///
|| bar est_unv year, barw(0.4) bc(black) ytitle(UNV Posts) xtitle(Year) xla(, tlc(none)) yscale(range(0 600)) legend(label(1 "Budgeted Posts") label(2 "Filled Posts (estimated)")) title("MONUC/MONUSCO")

	
*FIGURE 4
use "descriptives/mandate_budget_match.dta", replace
preserve
tab mission
keep if mission=="MONUC" | mission=="UNMIL" | mission=="UNAMID" | mission=="UNMOT"
tab umbrella1
keep if umbrella1=="Electoral Affairs"
egen missionid=group(mission)
bysort missionid: egen elect_ever=max(electoralaffairs_sum)
codebook elect_ever
keep if elect_ever>0
tsset missionid year
gen pipe = "|"
gen Election_Mandate=-50
twoway (scatter total year if elect_ever>0 & total!=0 & mission!="UNTAC", mcolor(gray) msize(medium) mlwidth(medium) msymbol(oh) lcolor(gray) lwidth(medium) by(mission) yscale(range(0 400))legend(label(1 "Election Posts Budgeted"))) (scatter total year if elect_ever>0 & total==0 & mission!="UNTAC", mcolor(gs13) msize(medium) mlwidth(medium) msymbol(X) lcolor(gs13) lwidth(medium) by(mission) yscale(range(0 400))  legend(label(2 "Election Posts not Budgeted")))  (scatter Election_Mandate year if total>0 & elect_ever>0 & mission!="UNTAC",  ms(none) mlabel(pipe) mlabpos(0) by(mission, note("")) yscale(range(0 400)) legend(label(3 "|    Election Mandate"))), yscale(range(0 400)) 
restore


*FIGURE 5
preserve	
keep if mission=="MONUC" | mission=="MONUSCO"
tab year
tab mission
drop if umbrella1=="Senior Civilian Leadership" | umbrella1=="Senior Uniformed Leadership" | umbrella1=="Support" | umbrella1=="Info Collection & Analysis" | umbrella1=="Internal Management"
egen umbrescale=group(umbrella1_sh)
egen umbid=group(umbrescale)
labmask umbid, values(umbrella1_sh)
tab umbid
sort umbid year
cap by umbid: gen time=_n
cap label var time "Year"
drop if match==. & umbid==19
twoway (scatter umbid time if match==1, msize(large) mlwidth(medium) ms(Oh) mcol(gs12) ylabel(1(1)18, valuelabel labsize(vsmall) angle(horizontal))) ///
(scatter umbid time if match==2, msize(large) mlwidth(medium)  ms(X) mcol(gs12) ylabel(1(1)18, 			valuelabel labsize(vsmall) angle(horizontal))) /// 
(scatter umbid time if match==3, msize(large) mlwidth(medium) ms(square) mcol(gray) ylabel(1(1)18, valuelabel labsize(vsmall) angle(horizontal))), aspectratio(.7) legend(position(bottom) label(1 "Mandated only") label(2 "Budgeted only") label(3 "Mandated & Budgeted") col(3)) title("MONUC/MONUSCO") ytitle("Area of Expertise") xlabel(1 "2001" 4 "2004" 8 "2008" 12 "2012" 16 "2016")
restore
